<template>
  <template v-if="isAlready">
    <el-container style="margin-bottom: -60px;">
      <el-header>
          <el-row :gutter="10" style="width: 100%;">
            <el-col :span="6">
              <el-form-item label="操作人" style="width: 250px;">
                <ex-selector :model="searchForm.documentInfo" prop="createUser" code="SysUser" :isicon="true"></ex-selector>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="处理方式">
                <ex-select v-model="searchForm.documentInfo.processTypeId" dict-code="FamsDocumentProcessType" pri-key placeholder="请选择处理方式" ></ex-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-button plain type="primary" icon="el-icon-search" @click="searchHandler" :loading="loading">
                {{ $t('button.search') }}
              </el-button>
              <el-button plain type="warning" icon="el-icon-brush" @click="resetHandler">{{ $t('button.reset') }}
              </el-button>
            </el-col>
          </el-row>
      </el-header>
      <el-main class="nopadding" style="padding-bottom: 15px;">
        <ex-table :id="'exTable'+widget.id" ref="assetLogTable" :height="380" :columns="columns" :url="apiUrl" size="small" :params="{assetId:model.assetId||model.id||'default'}" @columnClick="columnClickHandler" :selection="false"></ex-table>
      </el-main>
    </el-container>

    <data-card ref="dataCard"></data-card>
  </template>
</template>
<script>

import draggable from "vuedraggable";
import ExFormItem from "@/components/exFormItem/index.vue";
import ExTable from "@/components/exTable/index.vue";
import cache from "@/utils/cache";
import {defineAsyncComponent} from "vue";
import ExSelector from "@/components/exSelector/index.vue";
import ExSelect from "@/components/exSelect/index.vue";
import ExSearch from "@/components/exSearch/index.vue";


export default {
  name: "wAssetLog",
  computed: {
    cache() {
      return cache
    }
  },
  components: {
    ExSearch,
	  ExSelect,
	  ExSelector,
	  DataCard:defineAsyncComponent(()=>import("@/views/asset/card/card.vue")), ExTable, ExFormItem, draggable},
  props: {
    model: {type: Object, default: {}},
    widget: {type: Object, default: null},
    drag: {type: Boolean, default: false},
  },
  data() {
    return {
      isAlready: false,
      loading: false,
      apiUrl: null,
      searchForm:{documentInfo:{},changeBefore:{other:{}},changeAfter:{other:{}}},
      columns: [
        {code: "详细", name: "详细", width:"50",fixedValue:"详细",click:"toAsset"},
        {code: "documentInfo.code", name: "单据编号", width:"120",click:"toDocument"},
        {code: "documentInfo.documentConfigId", name: "单据类型", width:"100",formatter:"DICT",key: "FamsDocumentConfig"},
        {code: "documentInfo.processTypeId", name: "处理方式", width:"",formatter:"DICT",key: "FamsDocumentProcessType"},
        {code: "documentInfo.status", name: "单据状态", width:"75",formatter:"DICT",key: "DOCUMENT_STATUS"},
        {code: "changeAfter.code", name: "资产编号", width:"150"},
        {code: "changeAfter.assetStatusId", name: "资产状态", width:"80",formatter:"DICT",key: "ASSET_STATUS"},
        {code: "documentInfo.createUser", name: "操作人", width:"100",formatter:"DICT",key: "SysUser"},
        {code: "documentInfo.createTime", name: "操作时间", width:"165",formatter:"DATETIME"},

      ],
      dataCardShow:false,
    }
  },
  watch: {

  },
  created() {
    if(!this.drag){
      this.apiUrl = this.$API.asset.assetLog.list.url;
    }
    this.isAlready = true;
  },

  methods: {
    columnClickHandler(name,data){
      this.$options.methods[name](this, data);
    },
    async searchHandler() {
      await this.$refs.assetLogTable.refresh(this.searchForm);
    },
    resetHandler() {
      this.searchForm = {documentInfo:{},changeBefore:{other:{}},changeAfter:{other:{}}};
      this.searchHandler();
    },
    toDocument(_this,row){
      _this.$nextTick(() => {
        _this.$router.push({name:"documentPrepare",params:{fromRouteId:_this.$TOOL.crypto.MD5(_this.$route.path),configId:row.documentInfo.documentConfigId,id:row.documentId}});
      })
    },
    async toAsset(_this, row) {
      try {
        _this.loading = true;
        _this.dataCardShow = true;
        let rsp = await _this.$API.asset.document.findById.post({id: row.documentId});
        if (rsp.code === 2000) {
          let document = rsp.data;
          let entity = {modelId: row.documentId, cardId: document.processType.showCardId, _editFlag: false}
          if(row.documentDetailId){
            let res = await _this.$API.asset.documentDetail.findById.post({id: row.documentDetailId});
            if (res.code === 2000) {
              _this.$nextTick(() => {
                // console.log(row);
                _this.$refs.dataCard.open(Object.assign(entity, res.data));
              });
            }
          }
        }

      } finally {
        _this.loading = false;
      }
    },
    //导出
    async exp(_this, index) {
      let confirm = await this.$confirm('请确认是否执行此操作', '提示', {type: 'warning'}).then(() => {
        return true;
      }).catch(() => {
        return false;
      });
      if (!confirm) return;
      _this = this;
      _this.loading = true;
      let queryPrams = _this.$refs.table.queryParams();
      let res = await _this.$API.asset.documentDetail.export.post(queryPrams);
      if (res.code === 2000) {
        await _this.$alert(_this.$t('common.export_request_success'), "提示", {type: 'success'}).catch(() => {
        });
      }
      _this.loading = false;
    },
  }
}
</script>
<style>
.tableItemClass {
  cursor: move;
  float: left;
  background: #fff;
  height: 35px;
  text-align: center;
  horiz-align: center;
  border: 1px dashed #e6e6e6;
}

.tableItemClass:hover {
  border-color: rgb(71, 231, 250);
}

.tableDragAreaClass {
  height: 40px;
  background: rgba(243, 149, 95, 0.1);
}
</style>
